﻿using System;

/// <summary>
/// 可控随机数发生器
/// </summary>

namespace Model
{
    public static class SRandom
    {
        public static int count = 0;
        private static ulong randSeed { get; set; }
        /// <summary>
        /// 更新种子+返回随机数
        /// </summary>
        /// <returns>可控随机数</returns>
        public static uint Next()
        {
            randSeed = randSeed * 1103515245 + 12345;
            return (uint)(randSeed / 65536);
        }
        /// <summary>
        /// 随机数[0-(max-1)]
        /// </summary>
        /// <param name="max">最大值+1</param>
        /// <returns>uint类型随机数</returns>
        private static uint Next(uint max)
        {
            return Next() % max;
        }
        /// <summary>
        /// 随机数[0-(max-1)]
        /// </summary>
        /// <param name="max">最大值+1</param>
        /// <returns>int类型随机数</returns>
        private static int Next(int max)
        {
            return (int)(Next() % max);
        }

        /// <summary>
        /// 随机数[min-(max-1)]
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值+1</param>
        /// <returns>uint随机数</returns>
        public static uint Range(uint min, uint max)
        {
            if (min > max)
                throw new ArgumentOutOfRangeException("最小值", string.Format("'{0}'无法大于'{1}'.", min, max));

            uint num = max - min;
            return Next(num) + min;
        }
        /// <summary>
        /// 随机数[min-(max-1)]
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值+1</param>
        /// <returns>int随机数</returns>
        public static int Range(int min, int max)
        {
            count++;

            if (min > max)
                throw new ArgumentOutOfRangeException("最小值", string.Format("'{0}'无法大于'{1}'.", min, max));

            int num = max - min;

            return Next(num) + min;
        }
        /// <summary>
        /// 随机数[min-(max-1)]
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值+1</param>
        /// <returns>float随机数</returns>
        public static Fix64 Range(Fix64 min, Fix64 max)
        {
            if (min > max)
                throw new ArgumentOutOfRangeException("最小值", string.Format("'{0}'无法大于'{1}'.", min, max));

            uint num = (uint)(max.RawValue - min.RawValue);
            return Fix64.FromRaw(Next(num) + min.RawValue);
        }
    }
}
